package Day16;

import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.List;

public class BookHandler {
    public static void main(String[] args) {
        SAXReader saxReader=new SAXReader();
        try {
            Document document=saxReader.read(new FileInputStream("D:\\Users\\ld\\IdeaProjects\\Java高级\\Data\\Book.xml"));
            Element root=document.getRootElement();
            Book book=null;
            List<Element> books=root.elements("book");
            for(Element bookEl:books){
                String type=bookEl.attributeValue("type");
                String title=bookEl.elementText("title");
                String author=bookEl.elementText("author");
                String year=bookEl.elementText("year");
                String price=bookEl.elementText("price");
                Book book1=new Book();
                book1.setType(type);
                book1.setTitle(title);
                book1.setAuthor(author);
                book1.setYear(Integer.parseInt(year));
                book1.setPrice(Double.parseDouble(price));
                System.out.println(book1);
            }
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }
}
